package com.kitwee.serialport.data.model;

import java.io.FileDescriptor;

public class SerialPort {

    private SerialPortParam serialPortParam;

    public SerialPort(SerialPortParam serialPortParam) {
        this.serialPortParam = serialPortParam;
    }

    public FileDescriptor open() {
        return open(0);
    }

    public FileDescriptor open(int flags) {
        return open(serialPortParam.getDevicePath(), serialPortParam.getBaudRate(),
                serialPortParam.getParity().value(), serialPortParam.getDatabits().value(),
                serialPortParam.getStopbits().value(), flags);
    }

    // JNI
    public native FileDescriptor open(String path, int baudrate, int parity, int databits, int stopbits, int flags);

    public native void close(FileDescriptor fileDescriptor);

    static {
        System.loadLibrary("serial_port");
    }
}